home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / DIGEST_B / DOCUMENT / CBROWSER.C next >
Text File  |  1992-03-09  |  7KB  |  279 lines

  1. /****
  2.  * CBrowserDoc.c
  3.  *
  4.  *    Document methods for a typical application.
  5.  *
  6.  *  Copyright ⌐ 1990 Symantec Corporation.  All rights reserved.
  7.  *  Copyright ⌐ 1991 Manuel A. PÄrez.  All rights reserved.
  8.  *
  9.  ****/
  10.  
  11. #include "Global.h"
  12. #include "Commands.h"
  13. #include "CApplication.h"
  14. #include "CBartender.h"
  15. #include "CDecorator.h"
  16. #include "CDesktop.h"
  17. #include "CError.h"
  18. #include "CPanorama.h"
  19. #include "CScrollPane.h"
  20. #include "CBrowserDoc.h"
  21. #include "TBUtilities.h"
  22. #include "CWindow.h"
  23. #include <Packages.h>
  24. #include <string.h>
  25.  
  26. #define    WINDBrowser        500        /* Resource ID for WIND template */
  27.  
  28. /* Commands on the Index Menu */
  29. #define IndexFrom    200
  30. #define IndexDate    201
  31. #define IndexSubject 202
  32.  
  33. extern    CApplication *gApplication;    /* The application */
  34. extern    CBartender    *gBartender;    /* The menu handling object */
  35. extern    CDecorator    *gDecorator;    /* Window dressing object    */
  36. extern    CDesktop    *gDesktop;        /* The enclosure for all windows */
  37. extern    CBureaucrat    *gGopher;        /* The current boss in the chain of command */
  38. extern    OSType        gSignature;        /* The application's signature */
  39. extern    CError        *gError;        /* The global error handler */
  40.  
  41. /***
  42.  * IBrowserDoc
  43.  *
  44.  *    Does nothing, just calls the CDocument IDocument method
  45.  *
  46.  ***/
  47.  
  48.  
  49. void CBrowserDoc::IBrowserDoc(CApplication *aSupervisor, Boolean printable)
  50.  
  51. {
  52.     CDocument::IDocument(aSupervisor, printable);
  53.  
  54.     /* Initialize the default index displayed when the document is first
  55.      * created.  Ideally, we should read this from a resource.
  56.      */
  57.     index_displayed = IndexSubject;
  58.     itsListIndex = NULL;
  59.     itsMessage = NULL;
  60. }
  61.  
  62.  
  63. /***
  64.  * NewFile
  65.  *
  66.  * Empty implementation because we don't allow creation of new documents
  67.  ***/
  68. void CBrowserDoc::NewFile(void)
  69. { }    // This is correct, see note above.
  70.  
  71.  
  72. /***
  73.  * OpenFile
  74.  *
  75.  ***/
  76.  
  77. void CBrowserDoc::OpenFile(SFReply *macSFReply)
  78.  
  79. {
  80. Str63        theName;
  81. OSErr        theError;
  82. BrowserDir    dir;
  83.  
  84.  
  85.     // Copy pascal name to a c name
  86.     PtoCstr(macSFReply->fName);
  87.     strcpy(dir.fname, (char *)&macSFReply->fName);
  88.     CtoPstr(macSFReply->fName);
  89.  
  90.     // Copy the vref too
  91.     dir.vRefNum = macSFReply->vRefNum;
  92.  
  93.     // Then let this program do the work
  94.     BuildBrowserIndex(&dir);        // this could fail
  95.     BuildWindow(dir);
  96.     itsWindow->SetTitle(macSFReply->fName);
  97.     itsWindow->Select();            // Don't forget to make the window active
  98. }
  99.  
  100.  
  101. /******************************************************************************
  102.  Dispose
  103.  ******************************************************************************/
  104.  
  105. void CBrowserDoc::Dispose()
  106. {
  107.  
  108.     itsListIndex->Dispose();
  109.     itsMessage->Dispose();
  110.  
  111.     inherited::Dispose();
  112. }
  113.  
  114. /***
  115.  * BuildWindow
  116.  *
  117.  *    This is the auxiliary window-building method that the
  118.  *    NewFile() and OpenFile() methods use to create a window.
  119.  *
  120.  *    In this implementation, the argument is the data to display.
  121.  *
  122.  ***/
  123.  
  124. void CBrowserDoc::BuildWindow (BrowserDir dir)
  125. {
  126. CScrollPane        *theScrollPane;
  127. CScrollPane        *the2ndScrollPane;
  128. LongRect        theAperture;
  129. short            w;
  130.  
  131.             // const to help us understand the code
  132. #define scrollerHeight        120
  133. #define listHeight            (scrollerHeight-16)
  134. #define contentsVLoc        (scrollerHeight+1)
  135.  
  136.     /**    First create the window and initialize it. **/
  137.  
  138.     itsWindow = new(CWindow);
  139.     itsWindow->IWindow(WINDBrowser, FALSE, gDesktop, this);
  140.     itsWindow->GetAperture(&theAperture);
  141.     w = theAperture.bottom - theAperture.top - scrollerHeight;
  142.  
  143.     /** Create a scroll pane for the index field **/
  144.     theScrollPane = new(CScrollPane);
  145.     theScrollPane->IScrollPane(itsWindow, this, 10, scrollerHeight, 0, 0,
  146.                                 sizELASTIC, sizFIXEDSTICKY,
  147.                                 TRUE, TRUE, FALSE);
  148.     theScrollPane->FitToEnclFrame(TRUE, FALSE);
  149.  
  150.  
  151.     /**
  152.      **    itsMainPane is the document's focus
  153.      **    of attention. Some of the standard
  154.      **    classes (particularly CPrinter) rely
  155.      **    on itsMainPane pointing to the main
  156.      **    pane of your window.
  157.      **
  158.      **    itsGopher specifies which object
  159.      ** should become the gopher when the document
  160.      ** becomes active. By default
  161.      **    the document becomes the gopher. It╒s
  162.      **    likely that your main pane handles commands
  163.      **    so you╒ll almost always want to set itsGopher
  164.      **    to point to the same object as itsMainPane.
  165.      **
  166.      **/
  167.  
  168.     /** Create a DisplayIndex (top of the window) */
  169.     itsListIndex = new(CDisplayIndex);
  170.     itsGopher = itsListIndex;
  171.  
  172.     itsListIndex->IDisplayIndex(theScrollPane, this, 0, listHeight, dir, index_displayed);
  173.     theScrollPane->InstallPanorama(itsListIndex);
  174.  
  175.  
  176.     //**
  177.     // Build the contents of the message display
  178.     //**
  179.  
  180.     the2ndScrollPane = new(CScrollPane);
  181.     the2ndScrollPane->IScrollPane(itsWindow, this,
  182.             10, w,                    // sizes
  183.             0, contentsVLoc,        // locations
  184.             sizELASTIC, sizELASTIC,    // sizes types
  185.             TRUE, TRUE, TRUE);        // horiz, vert, and size box
  186.     the2ndScrollPane->FitToEnclFrame(TRUE, FALSE);        // horiz, vert
  187.  
  188.     itsMessage = new(CDisplayText);
  189.     itsMessage->IDisplayText(the2ndScrollPane, this, contentsVLoc, 1);
  190.     the2ndScrollPane->InstallPanorama(itsMessage);
  191.  
  192.     // itsMainPane is used for printing, so set the message (not the index)
  193.     // field as the main pane.
  194.     itsMainPane = itsMessage;
  195.  
  196.  
  197.     //
  198.     // Establish dependency between the two displays, by making
  199.     // the message display be dependent upon the selection on
  200.     // the index display
  201.     //
  202.     itsMessage->DependUpon(itsListIndex);
  203.  
  204.     gDecorator->PlaceNewWindow(itsWindow);
  205. }
  206.  
  207.             
  208. /***
  209.  * DoCommand {OVERRIDE}
  210.  *
  211.  *    Execute a command
  212.  *
  213.  ***/
  214.  
  215. void    CBrowserDoc::DoCommand(long theCommand)
  216. {
  217. long selLine;
  218.  
  219.     switch (theCommand) {
  220.         case IndexFrom:
  221.         case IndexDate:
  222.         case IndexSubject:
  223.             // Store index in an instance variable
  224.             index_displayed = theCommand;
  225.  
  226.             // Save the currently selected line            
  227.             selLine = itsListIndex->GetSelectedLine();
  228.  
  229.             // Set the index in the index display
  230.             itsListIndex->SetIndex(theCommand);
  231.  
  232.             // Setting the selected line back to what it was
  233.             itsListIndex->SetSelectedLine(selLine, true);
  234.             break;
  235.  
  236.         default:
  237.             inherited::DoCommand(theCommand);
  238.             break;
  239.     }
  240. }
  241.  
  242. /***
  243.  * UpdateMenus {OVERRIDE}
  244.  *
  245.  *    Enable all Index items, and place a check mark next to the current
  246.  *  one
  247.  *
  248.  ***/
  249.  
  250. void    CBrowserDoc::UpdateMenus()
  251. {
  252.     inherited::UpdateMenus();
  253.  
  254.     gBartender->EnableCmd(IndexFrom);
  255.     gBartender->EnableCmd(IndexDate);
  256.     gBartender->EnableCmd(IndexSubject);
  257.  
  258.     gBartender->CheckMarkCmd(IndexFrom, index_displayed == IndexFrom);
  259.     gBartender->CheckMarkCmd(IndexDate, index_displayed == IndexDate);
  260.     gBartender->CheckMarkCmd(IndexSubject, index_displayed == IndexSubject);
  261. }
  262.  
  263. /***
  264.  * ConfirmClose {OVERRIDE}
  265.  *
  266.  * The text edit panes make this document think that it has been modified.
  267.  * Since we don't support saving digest browsers, lets just fake it, and
  268.  * make it behave as if the user said it was ok to close the document
  269.  * without saving it.
  270.  *
  271.  * There might be a better way around this.  But this works nice.
  272.  ***/
  273.  
  274. Boolean    CBrowserDoc::ConfirmClose(Boolean quitting)
  275. {
  276. Boolean        okToClose = TRUE;
  277.     
  278.     return(okToClose);
  279. }